<?php
/**
 * 功能: 热销定时采集
 * User: WD
 * Date: 2018/10/30 0030
 * Time: 14:20
 */

namespace app\admin\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;

class Hot extends Command{
    protected function configure()
    {
        $this->setName('hot')->setDescription('热销榜');
    }

    protected function execute(Input $input, Output $output){
        $output->writeln("热销榜:");
        /*用不超时*/
        ini_set('max_execution_time', 0);
        $this->hot_collect();

    }
    public function hot_collect(){
    $vkey = config('vkey');
    $url = "http://api.vephp.com/products?vekey=" . $vkey;
    $result = file_get_contents($url);

    $arr = json_decode($result, true);
      /*echo "<pre>";
      print_r($arr);
      die;*/
    if (isset($arr['error'])) {
        return false;
    }
    $gb_id = Db::name('icon')->where('name', 'like', '%热销%')->value('id');
    if(empty($gb_id)){
        echo 'gb_id不能为空';
        return false;
    }
    if ($arr['data']) {
        $gb_goods = Db::name('goods')->where('gb_id', $gb_id)->select();
        //  dump($gb_goods);
        if($gb_goods){
            $ids = [];
            foreach($gb_goods as $k=>$v){
                $ids[] = $v['id'];
            }
            $a = Db::name('goods')->whereIn('id',$ids)->delete();
            //  dump($ids);
        }
        $goods = $arr['data'];
        $datas = $data = [];
        foreach ($goods as $k => $v) {
            $data['gb_id'] = $gb_id;
            $data['goods_add_time'] = time();
            $data['goods_id'] = $v['item_id'];//商品ID
            $data['goods_title'] = $v['title'];//标题
            $data['goods_detail_url'] = $v['pict_url'] ? $v['pict_url'] : '';//商品详情地址
            $data['goods_volume'] = $v['volume'];//月销量
            $data['goods_seller_id'] = $v['seller_id'];//供应商ID，卖方id
            $data['goods_description'] = $v['item_description'] ? $v['item_description'] : '';//供应商ID，卖方id
            $data['goods_price'] = $v['zk_final_price'];//商品价格
            $data['goods_z_price'] = $v['zk_final_price'];//折后价

           /* $data['goods_yj_rate'] = $v['commission_rate'];//佣金比例
            $data['goods_yongjin'] = round($v['zk_final_price'] * ($v['commission_rate'] / 10000), 1);//佣金
            $data['member_yongjin'] = $data['goods_yongjin'] / 2;//会员佣金*/

            $data['coupon_amount'] = $v['coupon_amount'];
            $data['goods_q_price'] = $data['goods_price'] - $data['coupon_amount'];
            $data['coupon_info'] = '满' . $v['coupon_start_fee'] . '元可用';
            $data['coupon_total_count'] = $v['coupon_total_count'];//优惠券数量
            $data['coupon_end_time'] = isset($v['coupon_end_time']) ? $v['coupon_end_time'] : 0;//优惠券结束时间
            $data['coupon_start_time'] = isset($v['coupon_start_time']) ? $v['coupon_start_time'] : 0;//优惠券开始时间
            $time = intval(time() + 72 * 86400);
            $data['goods_end_time'] = $v['coupon_end_time'] ? $v['coupon_end_time'] : $time;
            $data['goods_user_type'] = $v['user_type'];//卖家类型,1表示天猫

            $data['goods_yj_rate'] = $v['commission_rate'];//佣金比例
            if($data['goods_price']!=0 && $data['goods_z_price']!=0 && $data['goods_q_price']!=0){
                $min = min($data['goods_price'],$data['goods_z_price'],$data['goods_q_price']);
               // $data['goods_yongjin'] = round($min* ($v['commission_rate']/10000), 1);//佣金
                $data['goods_yongjin'] = round($min* ($v['commission_rate']/100), 1);//佣金
                $data['member_yongjin'] = $data['goods_yongjin']/2;//会员佣金
            }else{
            // $data['goods_yongjin'] = round($data['goods_price']* ($v['commission_rate']/10000), 1);//佣金
            $data['goods_yongjin'] = round($data['goods_price']* ($v['commission_rate']/100), 1);//佣金
            $data['member_yongjin'] = $data['goods_yongjin']/2;//会员佣金
        }

            $check_repeat = Db::name('goods')->where('goods_id', $data['goods_id'])->select();//检查重复
            // print_r($check_repeat);die;

            if (empty($check_repeat)) {
                $datas[] = $data;

            } else {

                Db::startTrans();
                try{
                    $success = Db::name('goods')->where('goods_id', '=', $data['goods_id'])->update($data);
                    // 提交事务
                    Db::commit();
                    echo '采集成功!';

                } catch (\Exception $e) {
                    // 回滚事务
                    Db::rollback();
                    echo $e->getMessage();
                }
            }

        }

        $res1 = second_array_unique_bykey($datas, 'goods_id');
        /*echo "<pre>";
        print_r($res1);
        die;*/

        if (!empty($res1)) {
            Db::startTrans();
            try{
                $success = Db::name('goods')->insertAll($res1);
                // 提交事务
                Db::commit();
                echo '采集成功';

            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
               echo $e->getMessage();
            }
        }
    }
}

}